﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Bloks
{
    class Player1
    {
        private Texture2D texture;
        private KeyboardState kbState;
        private Vector2 screenpos, origin, texturesize;
        private bool jumping = false;
        private float foerhop;
        private float rotation = 0;
        private float playerScale = 1;
        private float starttid = 0;


        public void LoadContent(GraphicsDevice device, Texture2D player_texture)
        {
            texture = player_texture;
            Viewport viewport = device.Viewport;
            // Set the origin so that we're drawing from the 
            // center of the top edge.
            origin = new Vector2(texture.Width / 2, texture.Height / 2);
            // Set the screen position to the center of the screen.
            screenpos = new Vector2(viewport.Width - 700, viewport.Height - 71);
            // Offset to draw the second texture, when necessary.
            texturesize = new Vector2(texture.Width, 0);
        }

        public void Update(double elapsed, Levels level)
        {
            kbState = Keyboard.GetState();


            if (kbState.IsKeyDown(Keys.Up))
            {
                playerScale += 0.1f;
            }

            if (kbState.IsKeyDown(Keys.Down))
            {
                playerScale -= 0.1f;
            }

            if (kbState.IsKeyDown(Keys.Space) && !jumping)
            {
                foerhop = screenpos.Y;
                jumping = true;
            }
            if (level.Collision(new Rectangle((int)screenpos.X, (int)screenpos.Y, texture.Width, texture.Height)))
            {
                rotation = 0;
                starttid = 0;
                //rammer blok
                jumping = false;

            }

            if (!jumping)
            {
                float test = level.Ingenblokunder(screenpos);
                if (screenpos.Y < test)
                {
                 screenpos.Y += 1;
                }
                if (screenpos.Y > 409 && test == 409)
                {
                    screenpos.Y = 409;
                }
            }

            if (jumping)
            {
                if (starttid == 0)
                {
                    starttid = (float)elapsed;
                }
                screenpos.Y -= -0.5f * 200f * ((float)elapsed - starttid) * ((float)elapsed - starttid) + 200 * (float)Math.Sin(MathHelper.ToRadians(15)) * ((float)elapsed - starttid);
                rotation += 4;
                if (screenpos.Y > foerhop)
                {
                    rotation = 0;
                    starttid = 0;
                    //rammer jorden
                    jumping = false;
                    screenpos.Y = foerhop;
                }
            }


        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, screenpos, null, Color.White, MathHelper.ToRadians(rotation), origin, playerScale, SpriteEffects.None, 0f);
        }
    }
}
